package client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import client.gui.panels.PanelIF;
import controllers.ControllerIF;
import controllers.MainPanelController;
import entities.User;

public class MainPanel extends JPanel implements PanelIF, ActionListener, TreeSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Instance of the panel's controller
	 */
	private MainPanelController mainPanelController;

	/**
	 * local panel - hosts any required panel
	 */
	private JPanel mainContentPanel;
	
	/**
	 * Scroll Bars inside the main content panel - wrap any given panel for enhanced display adjustment
	 */
	private JScrollPane mainContentScroller;

	/**
	 * reference of the program's main frame
	 */
	private MainFrame mainFrame;
	
	/**
	 * reference of the connected user
	 */
	private User connectedUser;
	
	private JPanel optionPanel;
	private JLabel usernameLbl;
	private JButton logoutButton;
	private JTree tree;


	/**
	 * Create the panel.
	 */
	public MainPanel(MainPanelController mainPanelController, MainFrame mainFrame) {
		this.mainFrame = mainFrame;
		this.mainPanelController = mainPanelController;
		this.mainPanelController.setMainPanel(this);
		this.mainContentScroller = new JScrollPane();
		initGui();
	}

	/**
	 * initialize the GUI
	 */ 
	private void initGui() {

		// setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setLayout(new BorderLayout());
		add(Box.createVerticalStrut(10));

		JPanel upperPanel = new JPanel();
		upperPanel.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		upperPanel.setLayout(new BoxLayout(upperPanel, BoxLayout.X_AXIS));
		upperPanel.setMaximumSize(new Dimension(mainFrame.getWidth(), 60));
		add(upperPanel, BorderLayout.NORTH);
		{
			upperPanel.add(Box.createHorizontalStrut(10));
			Date date = new Date();
			DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy   HH:mm:ss");
			String yourDate = dateFormat.format(date);

			JLabel dateTimeLbl = new JLabel(yourDate);
			upperPanel.add(dateTimeLbl);

			upperPanel.add(Box.createHorizontalGlue());

			JLabel logoLbl = new JLabel(" LOGO ");
			upperPanel.add(logoLbl);

			upperPanel.add(Box.createHorizontalGlue());

			usernameLbl = new JLabel(mainPanelController.getUserFirstName());
			upperPanel.add(usernameLbl);

			upperPanel.add(Box.createHorizontalStrut(10));

			logoutButton = new JButton("Disconnect");
			logoutButton.setToolTipText("Click here to log out from system");
			logoutButton.addActionListener(this);
			upperPanel.add(logoutButton);

			upperPanel.add(Box.createHorizontalStrut(10));
		}

		add(Box.createVerticalStrut(10));

		optionPanel = new JPanel();
		optionPanel.setMaximumSize(new Dimension(140, mainFrame.getHeight()));
		
		optionPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		optionPanel.setLayout(new GridLayout());
		add(optionPanel, BorderLayout.WEST);

		// Build the options tree in a side function
		JScrollPane treeScrollPanel = new JScrollPane();
		optionPanel.add(treeScrollPanel);
		tree = treeInit();
		tree.setSize(new Dimension(250, 700));
		tree.addTreeSelectionListener(this);
		treeScrollPanel.setViewportView(tree);
		optionPanel.add(treeScrollPanel, BorderLayout.WEST);

		mainContentPanel = new JPanel();
		mainContentPanel.setLayout(new BoxLayout(mainContentPanel, BoxLayout.Y_AXIS));
		mainContentPanel.setForeground(new Color(204, 204, 153));
		mainContentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		
			mainContentScroller = new JScrollPane();
			mainContentPanel.add(mainContentScroller);

		add(mainContentPanel, BorderLayout.CENTER);
		
	}
	
	/**
	 * Initialize the options tree according to the connected user's privileges
	 * @return JTree
	 */
	public JTree treeInit() {
		connectedUser = mainPanelController.getUser();
		final List<String> userPermissions = connectedUser.getPermissions();
		
		JTree tree;
		tree = new JTree();
		tree.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		tree.setSelectionRow(1);
		tree.setVisibleRowCount(20);
		tree.setModel(new DefaultTreeModel(
			new DefaultMutableTreeNode("ICM") {
				{
					DefaultMutableTreeNode node_1;
					DefaultMutableTreeNode node_2;
					add(new DefaultMutableTreeNode("Start new request"));
					add(new DefaultMutableTreeNode("Track ticket"));
					node_1 = new DefaultMutableTreeNode("Stuff");
						node_2 = new DefaultMutableTreeNode("View");
							node_2.add(new DefaultMutableTreeNode("List tickets as the ticket initiator"));
							if(userPermissions.contains("INFENG")) {
								node_2.add(new DefaultMutableTreeNode("List tickets as inspector"));
								node_2.add(new DefaultMutableTreeNode("List tickets as evaluator"));
								node_2.add(new DefaultMutableTreeNode("List tickets as execution leader"));
							}
							if(userPermissions.contains("CC") || userPermissions.contains("CM")) {
								node_2.add(new DefaultMutableTreeNode("List tickets as committe member"));
								node_2.add(new DefaultMutableTreeNode("List tickets as tester"));
							}
							node_1.add(node_2);
							if(userPermissions.contains("INFENG")) {
								node_2 = new DefaultMutableTreeNode("Requests");
									node_2.add(new DefaultMutableTreeNode("Time extension request"));
									node_2.add(new DefaultMutableTreeNode("Review time extensions requests"));
									node_2.add(new DefaultMutableTreeNode("View suspended tickets")); 
									node_2.add(new DefaultMutableTreeNode("View running tickets"));
								node_1.add(node_2);
							}
						add(node_1);
						if(userPermissions.contains("IFENGMNG")) {
						node_1 = new DefaultMutableTreeNode("Managment");
							node_1.add(new DefaultMutableTreeNode("Assigning inspector to tickets"));
							node_1.add(new DefaultMutableTreeNode("Replace inspector in ticket"));
							node_1.add(new DefaultMutableTreeNode("Replace committee members"));
							node_2 = new DefaultMutableTreeNode("Views");
								node_2.add(new DefaultMutableTreeNode("List all users"));
								node_2.add(new DefaultMutableTreeNode("View all tickets"));
								node_2.add(new DefaultMutableTreeNode("View all running tickets"));
								node_2.add(new DefaultMutableTreeNode("View all suspended tickets"));
								node_2.add(new DefaultMutableTreeNode("View all closed tickets"));
							node_1.add(node_2);
							node_2 = new DefaultMutableTreeNode("Reports");
								node_2.add(new DefaultMutableTreeNode("View reports"));
							node_1.add(node_2);
						add(node_1);
						}
				}
			}
		));
		return tree;
	}

	@Override
	public void setController(ControllerIF cif) {
		this.mainPanelController = (MainPanelController) cif;
	}

	/**
	 * Set the inner panel to display
	 * @param newPanel PanelIF
	 */
	public void setInnerPanel(PanelIF newPanel) {
		this.mainContentPanel.removeAll();
		this.mainContentScroller.setViewportView((JPanel)newPanel);
		this.mainContentPanel.add(mainContentScroller);
		this.mainContentPanel.validate();
		this.mainContentPanel.setVisible(true);
	}

	/**
	 * respond to button clicks
	 */
	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource().equals(logoutButton)) {
			mainFrame.logout();
		}
	}

	/**
	 * respond to tree nodes clicks
	 */
	@Override
	public void valueChanged(TreeSelectionEvent e) {
		mainPanelController.action();
	}
	
	/**
	 * return the line selected in the JTree
	 * @return
	 */
	public String getTreeSelection() {
		return tree.getLastSelectedPathComponent().toString();
	}

}
